package org.dromara.bean.domain.vo;


import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.experimental.Accessors;
import org.dromara.bean.config.LanguageEnum;
import org.dromara.bean.constant.CommonCode;
import org.dromara.bean.constant.CommonConstant;
import org.dromara.bean.constant.ReturnConstantEnum;
import org.dromara.bean.util.I18nMessageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data

@Component
@Accessors(chain = true)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private static Result result1;
    @Autowired
    private MessageSource messageSource;
    /**
     * 成功标志
     */

    private boolean success = true;
    /**
     * 返回处理消息
     */

    private String message = "操作成功！";
    /**
     * 返回代码
     */

    private Integer code = 200;
    /**
     * 返回数据对象 data
     */

    private Object result;
    /**
     * 时间戳
     */

    private long timestamp = System.currentTimeMillis();

    public Result() {

    }


    public static Result<Object> OK(String message) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(message);
        return r;
    }

    public static Result<Object> result(String value) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("成功");
        r.setResult(value);
        return r;
    }

    public static Result<Object> OK() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("成功");
        return r;
    }

    public static Result<Object> OK(Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public static Result<Object> ok() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("成功");
        return r;
    }

    public static Result<Object> ok(String code) {
        Result<Object> r = new Result<Object>();
        String message = "代码正在更新中，请稍等...";
        if (!code.matches("^[0-9]*$")) {
            message = result1.messageSource.getMessage("10007", null, LocaleContextHolder.getLocale());
        } else {
            message = result1.messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
        }
        r.setSuccess(true);
        r.setMessage(message);
        r.setCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<Object>();
        String msg = result1.messageSource.getMessage(CommonCode.SMART_HOSPITAL_SUCCESS, null, LocaleContextHolder.getLocale());
        r.setSuccess(true);
        r.setMessage(msg);
        r.setCode(200);
        r.setResult(data);
        return r;
    }

    public static Result<Object> ok(String code, Object obj) {
        Result<Object> r = new Result<Object>();
        String message = "代码正在更新中，请稍等...";
        if (!code.matches("^[0-9]*$")) {
            message = result1.messageSource.getMessage("10009", null, LocaleContextHolder.getLocale());
        } else {
            message = result1.messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
        }
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(obj);
        r.setMessage(message);
        System.out.println(r);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> errorCode(String code) {
        Result<Object> r = new Result<Object>();
        String message = "代码正在更新中，请稍等...";
        if (!code.matches("^[0-9]*$")) {
            message = result1.messageSource.getMessage("10009", null, LocaleContextHolder.getLocale());
        } else {
            message = result1.messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
        }
        return error(Integer.parseInt(code), message);
    }

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
    }

    /**
     * 通过@PostConstruct实现初始化bean之前进行的操作
     *
     * @desc 初始化操作，得到QuartzManager实例
     * @Date 2019年1月7日
     */
    @PostConstruct
    public void init() {
        result1 = this;
        result1.messageSource = this.messageSource;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }

    /**
     * api 返回结果,区分多语言
     *
     * @param language 语言类型,eg: en_us 表示美式英文
     */
    public Result<Object> ok_en(String msg, String language) {
        language = LanguageEnum.getLanguageType(language);
        try {
            this.message = I18nMessageUtil.getMessage(language, ReturnConstantEnum.SUCCESS.getMessage(), "SUCCESS");
        } catch (IOException e) {
            this.message = msg;
        }
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        return r;
    }

//	public static MessageSource getMessage() {
//		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//		messageSource.setCacheSeconds(-1);
//		messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
//		messageSource.setBasenames("/i18n/message");
//
//		return messageSource;
//	}
}
